<template>
  <div>
    <el-breadcrumb separator="/" style="height:10px;">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
      <el-table
      :data="pageinfo.list"
      border
      style="width: 100%">
      <el-table-column
        prop="uname"
        label="姓名"
        width="180">
      </el-table-column>

      <el-table-column label="性别" width="180">
        <template slot-scope="scope">
          {{scope.row.gender == 0 ? '男':'女'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="iphone"
        label="手机号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="certificateType.tname"
        label="护照"
        width="180">
      </el-table-column>
      <el-table-column
        prop="idnumber"
        label="证件号码"
        width="180">
      </el-table-column>

      <el-table-column label="状态" width="180">
        <template slot-scope="scope">
          {{scope.row.state == 0 ? '已删除':'未删除'}}
        </template>
      </el-table-column>
      <el-table-column label="管理操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row.uid)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.uid)">删除</el-button>
        </template>
      </el-table-column>
  </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userVo.pageNum"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="userVo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageinfo.total">
    </el-pagination>

    <!-- 修改 弹框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
       <el-form :model="ruser" label-width="80px">
            <el-form-item label="用户名" prop="uname">
            <el-input v-model="ruser.uname" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="upassword">
            <el-input v-model="ruser.upassword" placeholder="请输入密码" show-password></el-input>
            </el-form-item>
            <!-- <el-form-item label="确认密码">
            <el-input v-model="ruser.quedingupassword" placeholder="请再次输入密码" show-password></el-input>
            </el-form-item> -->
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="ruser.gender">
                <el-radio :label="0">男</el-radio>
                <el-radio :label="1">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="手机号"  prop="iphone">
            <el-input v-model="ruser.iphone" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item label="证件类型">
            <el-select v-model="ruser.typeid" placeholder="请选择证件类型" prop="region">
                <el-option v-for="(idtype,index) in idtypelist" :key="index" :label="idtype.tname" :value="idtype.tid"></el-option>
            </el-select>
            </el-form-item>
            <el-form-item label="证件号码" prop="idnumber">
            <el-input v-model="ruser.idnumber" placeholder="请输入证件号码"></el-input>
            </el-form-item>
            
            <el-form-item>
            <el-button type="success" @click="updateUserFn">修改</el-button>
            <el-button @click="quxiao">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {findAllUserForpage,findOneUserByuid,findAllidtype,updateuser,delUserByuid} from '../api'
export default {
  data() {
    return {
      pageinfo : {},
      userVo : {
        pageNum : 1,
        pageSize : 5
      },
      dialogVisible : false,
      ruser : {},
      idtypelist : []
    }
  },
  created() {
    this.findAllBypage();
  },
  methods: {
    async findAllBypage(){
      let {data:baseRefault} = await findAllUserForpage(this.userVo);
      this.pageinfo = baseRefault.data
    },
    handleSizeChange(val) {
      this.userVo.pageNum = 1
      this.userVo.pageSize = val
      this.findAllBypage();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.userVo.pageNum = val
      this.findAllBypage();
      // console.log(`当前页: ${val}`);
    },
    async handleEdit(index,uid){
      var staffname = sessionStorage.getItem('staffid')
      if(staffname == 'admin'){
        console.info(index + 'aaa'  + uid)
        let {data:baseRefault} = await findOneUserByuid(uid);
        this.ruser = baseRefault.data

        let {data:baseRefaut} = await findAllidtype()
        this.idtypelist = baseRefaut.data

        this.dialogVisible = true
      }else{
        this.$message.error('您无权修改用户信息！请联系管理员！')
      }
      
    },
    async updateUserFn(){
      let {data:baseRefaut} = await updateuser(this.ruser)
      this.dialogVisible = false
      this.$message.success(baseRefaut.message)
      this.findAllBypage();
    },
    quxiao(){
      this.$message.warning('取消修改')
      this.dialogVisible = false
    },
    async handleDelete(uid){
      var staffname = sessionStorage.getItem('staffid')
      if(staffname == 'admin'){
        let {data:baseRefaut} = await delUserByuid(uid)
        this.$message.success(baseRefaut.message)
        this.findAllBypage();
      }else{
        this.$message.error('您无权进行注销用户操作！请联系管理员！')
      }
      
    }
  },
}
</script>

<style>

</style>